package com.ddd.adapter.api.web;

import com.ddd.client.user.api.IUserService;
import com.ddd.client.user.dto.command.UserRegisterCmd;
import com.ddd.client.user.dto.co.UserCO;
import com.ddd.client.user.dto.query.UserListByParamQuery;
import com.ddd.client.user.dto.query.UserLoginQuery;
import com.ddd.framework.dto.MultiResponse;
import com.ddd.framework.dto.Response;
import com.ddd.framework.dto.SingleResponse;
import com.ddd.infrastructure.common.annotation.DddLog;
import com.ddd.infrastructure.common.core.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户控制器
 *
 * @author guowm
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("user")
@Tag(name = "用户接口")
public class UserController extends BaseController {

    private final IUserService userService;

    @PostMapping(value = "/register")
    @Operation(summary = "注册")
    @DddLog(description = "用户注册")
    public SingleResponse<UserCO> register(@RequestBody UserRegisterCmd userRegisterCmd) {
        return SingleResponse.success(userService.register(userRegisterCmd));
    }

    @PostMapping(value = "/login")
    @Operation(summary = "登录")
    public SingleResponse<Map<String, String>> login(@RequestBody UserLoginQuery userLoginQuery) {
        String jwtToken = userService.login(userLoginQuery);
        return SingleResponse.success(new HashMap<>(16) {{
            put("accessToken", jwtToken);
        }});
    }

    @GetMapping(value = "/list")
    @Operation(summary = "列表")
    @DddLog(description = "用户列表")
    public MultiResponse<UserCO> list(UserListByParamQuery userListByParamQuery) {
        startPage();
        return MultiResponse.success(userService.listByParam(userListByParamQuery));
    }

    @DeleteMapping(value = "/delete")
    @Operation(summary = "删除")
    @DddLog(description = "用户删除")
    public Response delete(String[] ids) {
        return Response.of(userService.deleteByIds(ids));
    }
}